/**
 *  常用的24种TS类型
 *    基本类型：
 *      number，string，boolean，symbol，null，undefined
 *
 *    根类型：
 *      Object， {}
 *
 *    对象类型：
 *      Array，object，function
 *
 *    枚举：
 *      enum:
 *        好处：
 *          1. 有默认值和可以自增值，节省编码时间
 *          2. 语义更清晰，可读性增强，方式参数类型类型可以明确为枚举类型
 *
 *     其他特殊类型：
 *      any，unknown，never，void，元组（tuple），可变元组
 *
 *    合作类型：
 *      联合类型，交叉类型
 *
 *    字面量类型
 *
 * */

// Object: 根类型，除了null，undefined不能直接赋值给Object，其他类型都可以赋值给Object根类型
let data: Object = "333";

// {}: Object跟类型的简写
let data2: {} = 1212;

// object: 只有对象类型才能赋值给他
let data3: object = { name: "zs" };

// 联合类型
let str: string | number = "abc";
str = 12;

console.log(str);

// 交叉类型
type TObj = { username: string };
type TObj1 = { age: number };
type TObj3 = TObj & TObj1;

let obj: TObj = { username: "aaa" };
let obj2: TObj1 = { age: 12 };
let obj3: TObj3 = {
  username: "ww",
  age: 12,
};

// 字面量数据类型
type increaseFlag = 0 | 1;
function isStartUp(increase: increaseFlag) {
  if (increase === 1) {
    console.log("open");
  } else {
    console.log("close");
  }
}

isStartUp(1);
isStartUp(0);

// never: 预留出之后要写的数据类型，保证代码健壮性
type DataFlow = string | number;
function dataFlowAnalysisWithNever(dataFlow: DataFlow) {
  if (typeof dataFlow === "string") {
    console.log("string", dataFlow.length);
  } else if (typeof dataFlow === "number") {
    console.log("number", dataFlow.toFixed(2));
  } else {
    let data = dataFlow;
  }
}

dataFlowAnalysisWithNever("abc");
dataFlowAnalysisWithNever(111);
// dataFlowAnalysisWithNever(boolean)

// 枚举
// 数字枚举
enum Week {
  Monday = 1,
  Tuesday,
  Wensday,
  Thirsday,
  Firday,
  Saturday,
  Sunday,
}
// 数字类型枚举可以反向取值：Week[1]
console.log(Week.Monday, Week[1], Week["Monday"]);

// 字符串枚举
enum WeekStr {
  Monday = "Monday",
  Tuesday = "Tuesday",
  Wensday = "Wensday",
  Thirsday = "Thirsday",
  Firday = "Firday",
  Saturday = "Saturday",
  Sunday = "Sunday",
}

console.log(WeekStr.Monday);

export {};
